/**
 * 根据身份证号计算年龄和性别
 * @param {身份证号} UUserCard
 * @obj {sex, age}
 */
export function discriCard(UUserCard) {
  UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
  // 获取性别
  let sex = ""
  if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
    // 是男则执行代码 ...
    sex = "男"
  } else {
    // 是女则执行代码 ...
    sex = "女"
  }
  //获取年龄
  let myDate = new Date();
  let month = myDate.getMonth() + 1;
  let day = myDate.getDate();
  let age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
  if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
    age++;
  }
  //年龄 age
  return {
    sex,
    age
  }
}
/**
 * 初始时间
 * @return 传入的时间今天的日期 和 7天后的日期
 */
export function oneDateGetNextSevenDate(odate) {
  let date = odate ? new Date(odate) : new Date();
  let timeStamp = date.getTime();
  let nextTimeStamp = timeStamp + 7 * 24 * 60 * 60 * 1000;
  return {
    date: date2YYYYMMDD(timeStamp).type1,
    nextDate: date2YYYYMMDD(nextTimeStamp).type1
  }
}
/**
 * 传一个时间
 * 变化为yyyy-MM-dd格式type1
 * 变化为yyyy/MM/dd格式type2
 */
export function date2YYYYMMDD(odate) {
  let date = odate ? new Date(odate) : new Date();
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString();
  if (month.length == 1) {
    month = '0' + month
  }
  let day = (date.getDate()).toString();
  if (day.length == 1) {
    day = '0' + day
  }
  return {
    "type1": year + '-' + month + '-' + day,
    "type2": year + '/' + month + '/' + day
  };
}

/**
 * 传一个时间得到星期几
 */
export function date2day(odate) {
  let date = odate ? new Date(odate) : new Date();
  let day = date.getDay();
  let dayStr = "";
  switch (day) {
    case 1:
      {
        dayStr = "星期一";
        break;
      }
    case 2:
      {
        dayStr = "星期二";
        break;
      }
    case 3:
      {
        dayStr = "星期三";
        break;
      }
    case 4:
      {
        dayStr = "星期四";
        break;
      }
    case 5:
      {
        dayStr = "星期五";
        break;
      }
    case 6:
      {
        dayStr = "星期六";
        break;
      }
    case 0:
      {
        dayStr = "星期日";
        break;
      }
  }
  return dayStr;
}
/**
 * 传一个时间得到后七天的时间的一个数组
 */
export function date2sevenDates(odate) {
  let date = odate ? new Date(odate) : new Date();
  let arr = [];
  for (let i = 0; i < 7; i++) {
    arr.push(date2YYYYMMDD(new Date(date).getTime() + i * 24 * 60 * 60 * 1000).type1);
  }
  return arr;
}

//时间格式的函数 yyyy-mm-dd hh:mm:ss
export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}